package mr;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapTask extends Thread{
    private File file;
    private int flag;

    public MapTask(File file, int flag){
        this.file=file;
        this.flag=flag;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            while ((line=br.readLine())!=null){
                String clazz = line.split(",")[4];
                if(!map.containsKey(clazz)){
                    map.put(clazz,1);
                }else {
                    map.put(clazz,map.get(clazz)+1);
                }
            }
            br.close();
            BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\IDEJAVA\\mapreduce\\prt\\part---" + flag));
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            for (Map.Entry<String, Integer> enter: entries) {
                String key = enter.getKey();
                Integer value = enter.getValue();
                bw.write(key+","+value);
                bw.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
